// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import ScrollTextItemGroup from "./ScrollTextItemGroup";

const { ccclass, property, requireComponent } = cc._decorator;
@ccclass
@requireComponent(cc.Layout)
export default class ScrollText extends cc.Component {

    @property(cc.Prefab)
    itemPrefab: cc.Prefab = null;

    private _layout: cc.Layout;
    private _mask: cc.Mask;
    private _itemGroups: cc.Node[] = [];
    private _itemGroupScript: ScrollTextItemGroup[] = [];
    private _pool: cc.Node[] = [];
    private _inited: boolean = false

    onLoad() {
        this.initLayoutMask()
    }

    initLayoutMask() {
        if (this._inited) {
            return;
        }
        this._inited = true;
        this._layout = this.node.getComponent(cc.Layout);
        this._mask = this.node.getComponent(cc.Mask);
        this._layout.type = cc.Layout.Type.HORIZONTAL
        this._layout.resizeMode = cc.Layout.ResizeMode.CONTAINER;
        this._layout.horizontalDirection = cc.Layout.HorizontalDirection.RIGHT_TO_LEFT;
        this._mask.type = cc.Mask.Type.RECT;
    }

    start() {
        if (this._itemGroups.length < this.node.childrenCount) {
            for (let i = 0; i < this.node.children.length; i++) {
                let node = this.node.children[i]
                this.addItemGroup("0", node)
            }
        }

        this.data = "111";
        this.schedule(() => {
            this.data = (Number(this.data) + 3) + ""
        }, 3)
    }

    private removeItemGroup(index: number) {
        if (this._itemGroupScript[index]) {
            if (!CC_EDITOR) {
                this._pool.push(this._itemGroups[index])
            }
            this._itemGroupScript[index].recycle();
            this._itemGroupScript.splice(index, 1)
            this._itemGroups.splice(index, 1)
        }
    }

    private addItemGroup(value: string, node: cc.Node = null) {
        if (!node && this._pool) {
            node = this._pool.pop();
        }
        if (!node) {
            node = new cc.Node("ScrollTextItemGroup");
        }
        if (node.parent != this.node) {
            node.parent = this.node;
        }
        node.y = 0;
        let scrollTextItemGroup = node.getComponent(ScrollTextItemGroup);
        if (!scrollTextItemGroup) {
            scrollTextItemGroup = node.addComponent(ScrollTextItemGroup)
        }
        if (!this._itemGroups.includes(node)) {
            this._itemGroups.push(node)
        } else {
            console.error("this._itemGroups.includes(node)")
        }
        if (!this._itemGroupScript.includes(scrollTextItemGroup)) {
            this._itemGroupScript.push(scrollTextItemGroup)
        } else {
            console.error("this._itemGroupScript.includes(scrollTextItemGroup)")
        }
        let index = this._itemGroups.indexOf(node);
        let height = this.node.height
        scrollTextItemGroup.init(this.itemPrefab, index, value, height)
    }
    @property
    private _data: string = "";
    @property
    public set data(value: string) {
        if (value == this._data) {
            return;
        }
        this._data = value;
        let str = value.toString();
        let len = str.length;
        if (CC_EDITOR) {
            this.initLayoutMask()
            this.removeAllItemGroup();
        }

        if (this._itemGroups.length > len) {
            let cutNum = this._itemGroups.length - len;
            for (let i = 0; i < cutNum; i++) {
                this.removeItemGroup(this._itemGroups.length - 1)
            }
        }
        for (let i = 0; i < len; i++) {
            let oneStr = str[len - i - 1];
            if (!this._itemGroups[i]) {
                this.addItemGroup(oneStr)
            } else {
                let withAction = true;
                if(CC_EDITOR){
                    withAction = false
                }
                this._itemGroupScript[i].setValue(oneStr,withAction)
            }
        }
    }

    private removeAllItemGroup() {
        for (let i = this._itemGroups.length - 1; i > -1; i--) {
            this.removeItemGroup(i)
        }
        if(CC_EDITOR){
            this.node.removeAllChildren();
        }
    }

    public get data() {
        return this._data;
    }

    // update (dt) {}
}
